<template>
  <div class="task-group">
    <div class="task-group__header">
      <div class="task-group__header-wrapped">
        <div class="task-group__header-nav">
          <div
            v-for="item in category"
            :key="item.id"
            :class="[item.id === active ? 'is-active' : '']"
            class="task-group__header-nav_item"
            @click="onSelect(item.id)"
          >
            <span
              v-if="item.hasSymptoms"
              class="task-group__header-nav_item-dot"
            ></span>
            {{ item.name }}
            <span
              v-show="item.id === active"
              class="task-group__header-nav_item-line"
            ></span>
          </div>
        </div>
      </div>
    </div>
    <div class="task-group__content">
      <div class="task-group__progress">
        <div class="task-group__progress-label">
          {{ missionNeedData.name }}
        </div>
        <CommonProgress :percentage="missionNeedData.completedPercent * 100" />
        <div class="task-group__progress-num">
          {{ missionNeedData.completedCount }}/{{
            missionNeedData.targetCount
          }}条
        </div>
      </div>

      <TaskTree :data="catalogList" @preview="onPreview" />
    </div>
  </div>
</template>

<script setup lang="ts">
import CommonProgress from "@/components/common-progress/index.vue";
import TaskTree from "./task-tree.vue";
import { computed, ref, watch } from "vue";
import {
  ICatalogItem,
  ICaterogyItem,
  ISelfMissionItem,
} from "@/apis/types/knowledge";
import { apiQueryCatalogById, apiQueryCatalogProgress } from "@/apis/knowledge";
import { ResponseCodeEnum } from "@/constants/index";

defineOptions({
  name: "task-group",
});

const props = withDefaults(
  defineProps<{
    updated: boolean;
    category: ICaterogyItem[];
  }>(),
  {
    category: () => [],
    updated: false,
  }
);

const emits = defineEmits(["preview"]);

const active = ref<number>(0);
const catalogList = ref<ICatalogItem[]>([]);
const missionData = ref<ISelfMissionItem | null>(null);

const missionNeedData = computed(() => {
  let defaultName = "--";
  let defaultPercent = 0;
  let defaultTargetValue = 0;
  let defaultCompletedCount = 0;
  if (missionData.value) {
    const { completedPercent, targetCount, completedCount, name } =
      missionData.value;
    defaultName = name;
    defaultPercent = completedPercent;
    defaultTargetValue = targetCount;
    defaultCompletedCount = completedCount;
  }

  return {
    completedPercent: defaultPercent,
    targetCount: defaultTargetValue,
    completedCount: defaultCompletedCount,
    name: defaultName,
  };
});

watch(
  () => props.category,
  (val) => {
    if (val && val.length && !active.value) {
      active.value = val[0].id;
    }
  }
);

watch(
  () => active.value,
  (val) => {
    if (val) {
      queryMissionServer(val);
      queryMissionProgress(val);
    }
  }
);

watch(
  () => props.updated,
  (val) => {
    if (val) {
      queryMissionProgress(active.value);
    }
  }
);

const onSelect = (value: number) => {
  if (active.value !== value) {
    active.value = value;
  }
};

const onPreview = (id: number) => {
  emits("preview", id);
};

const queryMissionServer = (categoryId: number) => {
  apiQueryCatalogById(categoryId).then((res: any) => {
    const { code, data } = res;
    if (code === ResponseCodeEnum.SUCCESS) {
      catalogList.value = data;
    }
  });
};

const queryMissionProgress = (categoryId: number) => {
  apiQueryCatalogProgress(categoryId).then((res: any) => {
    const { code, data } = res;
    if (code === ResponseCodeEnum.SUCCESS) {
      missionData.value = data;
    }
  });
};
</script>

<style lang="less">
.task-group {
  width: 100%;
  border-radius: 16px;
  padding: 24px 0;
  box-sizing: border-box;
  background: white;
  &__header {
    border-bottom: 1px solid #d8d8d8;
    padding: 0 32px;

    &-wrapped {
      width: 100%;
      overflow-x: auto;
      overflow-y: hidden;
      &::-webkit-scrollbar {
        display: none;
      }
    }
    &-nav {
      display: flex;
      gap: 30px;
      &_item {
        flex-grow: 0;
        flex-shrink: 0;
        position: relative;
        font-weight: 400;
        font-size: 28px;
        color: #8b8b8b;
        line-height: 40px;
        height: 50px;
        &.is-active {
          font-weight: 500;
          font-size: 32px;
          color: #000000;
          line-height: 44px;
        }
        &-dot {
          width: 10px;
          height: 10px;
          background: #ff5a5a;
          border-radius: 50%;
          position: absolute;
          right: -10px;
          top: 0;
        }
        &-line {
          position: absolute;
          transform: translate(-50%, 0);
          left: 50%;
          bottom: 0;
          width: 34px;
          height: 6px;
          background: #6acbb9;
          border-radius: 8px;
        }
      }
    }
  }
  &__content {
    width: 100%;
    padding: 42px 32px 0 32px;
    box-sizing: border-box;
  }

  &__progress {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 24px;
    &-label,
    &-num {
      flex-shrink: 0;
      flex-grow: 0;
      // width: 170px;
    }
    &-label {
      font-weight: 500;
      font-size: 32px;
      color: #000000;
      line-height: 44px;
    }
    &-num {
      font-weight: 400;
      font-size: 26px;
      color: #8b8b8b;
      line-height: 34px;
    }
  }
}
</style>
